<?php
/*
Copyright 2011 Erick Martínez Sámano

This file is part of PHP Quick Calendar E.

PHP Quick Calendar E is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PHP Quick Calendar E is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

<http://www.gnu.org/licenses/>
*/
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class pqcesController extends JController
{
	function __construct()
	{
		parent::__construct();
		$this->registerTask( 'addCategory'  , 	'editCategory' );
		$this->registerTask( 'close'  , 	'showCategory' );
		$this->registerTask( 'addEvent'  , 	'editFact' );
		$this->registerTask( 'closeFact',	'display' );
	}

	function display()
	{
		parent::display();
	}
	
	function showCategory()
	{
		JRequest::setVar( 'view', 'categorias' );
		parent::display();
	}
	function editCategory()
	{
		JRequest::setVar('view','nuevacat');
		$array = JRequest::getVar('cid',  0, '', 'array');
		JRequest::setVar('cid',(int)$array[0]);
		parent::display();
	}
	function saveCategory()
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		$model= $this->getModel('nuevacat');
		$model->store();
		JRequest::setVar( 'view', 'categorias' );
		parent::display();
	}
	function toggleActive()
	{
		JRequest::checkToken('get') or die( 'Invalid Token' );
		$act=JRequest::getVar('active',0);
		$arr = JRequest::getVar('cid',  0, '', 'array');
		$data=array('active'=>$act,'cid'=>(int)$arr[0],'id'=>(int)$arr[0]);
		$model= $this->getModel('nuevacat');
		$model->toggle($data);
		JRequest::setVar( 'view', 'categorias' );
		parent::display();
	}
	function remove()
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		$model = $this->getModel('nuevacat');
		if(!$model->delete()) {
			$msg= $model->getError();
			if (!$msg) {$msg = JText::_( 'COM_PQCE_DELITEMSERR' );}
		} else {
			$msg = JText::_( 'COM_PQCE_DELITEMS' );
		}
		$this->setRedirect(JRoute::_('index.php?option=com_pqce').'&view=categorias', $msg );
	}
	function editFact()
	{
		JRequest::setVar( 'view', 'nuevoac' );
		parent::display();
	}
	function applyFact(){
		$id=$this->saveFact(false);
		($id)? $this->setRedirect(JRoute::_('index.php?option=com_pqce').'&view=nuevoac&cid[]='.$id):'';
	}
	function saveFact($p=true)
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		$id = JRequest::getVar('id',  0, 'post', 'int');
		JRequest::setVar('cid',  $id);
		$model= $this->getModel('nuevoac');
		if (!($r=$model->store())) {
			$return=JRoute::_('index.php?option=com_pqce').'&view=nuevoac';
			($id)? $return.="&cid[]=$id": '';
			$this->setRedirect($return,$model->getError(),'error');
			return 0;
			}
		if ($p){
		$msg=JText::_('COM_PQCE_SAVECHANGES');
		$this->setRedirect(JRoute::_('index.php?option=com_pqce'),$msg);}
		return $r;
	}
	function remFact()
	{
		JRequest::checkToken() or die( 'Invalid Token' );
		$model = $this->getModel('nuevoac');
		if(!$model->delete()) {
			$msg= $model->getError();
			if (!$msg) {$msg = JText::_( 'COM_PQCE_DELITEMSERR' );}
		} else {
			$msg = JText::_( 'COM_PQCE_DELITEMS' );
		}
		$this->setRedirect(JRoute::_('index.php?option=com_pqce'), $msg );
	}
	function toggleActiveFact()
	{
		JRequest::checkToken('get') or die( 'Invalid Token' );
		$act=JRequest::getVar('active',0);
		$arr = JRequest::getVar('cid',  0, '', 'array');
		$data=array('active'=>$act,'cid'=>(int)$arr[0],'id'=>(int)$arr[0]);
		$model= $this->getModel('nuevoac');
		$model->toggle($data);
		parent::display();
	}
}
